package com.eboss2.sbs.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;
import com.eboss2.sbs.interfaces.StoreCallbackListener;
import com.eboss2.sbs.model.StoreManageInfo;
import com.mytools.tool.utils.StringUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.List;

public class ServiceManagerAdapter extends BaseAdapter {

	private Context context;
	private List<StoreManageInfo> datas;
	private LayoutInflater inflater;
	private ViewHolder holder = null;
	private StoreCallbackListener listener;

	public ServiceManagerAdapter(Context context, List<StoreManageInfo> datas, StoreCallbackListener listener) {
		this.context = context;
		this.datas = datas;
		this.inflater = LayoutInflater.from(context);
		this.listener = listener;
	}

	@Override
	public int getCount() {
		if(datas!=null&&datas.size()>0)return datas.size();
		else return 0;
	}

	@Override
	public StoreManageInfo getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		


		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_store_goods, null);
			
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.tv_category = (TextView) convertView.findViewById(R.id.tv_category);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
			holder.tv_kc = (TextView) convertView.findViewById(R.id.tv_kc);
			holder.tv_xl = (TextView) convertView.findViewById(R.id.tv_xl);
			holder.tv_new_amt = (TextView) convertView.findViewById(R.id.tv_new_amt);
			holder.tv_old_amt = (TextView) convertView.findViewById(R.id.tv_old_amt);
//			holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
			holder.tv_btn_1 = (TextView) convertView.findViewById(R.id.tv_btn_1);
			holder.tv_btn_2 = (TextView) convertView.findViewById(R.id.tv_btn_2);
			holder.tv_btn_3 = (TextView) convertView.findViewById(R.id.tv_btn_3);
			holder.ll_button = (LinearLayout) convertView.findViewById(R.id.ll_button);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}


		final StoreManageInfo data = datas.get(position);


		if (data.getIsLaunch() == 1) {
			holder.tv_time.setText("上架时间: "+data.getGmtModified());
			holder.tv_btn_3.setText("下架");
			holder.tv_btn_1.setVisibility(View.VISIBLE);
		}else{
			holder.tv_time.setText("入库时间: "+data.getGmtModified());
			holder.tv_btn_3.setText("上架");
			holder.tv_btn_1.setVisibility(View.GONE);
		}
		StringBuilder builder = new StringBuilder();
		builder.append(StringUtils.isBlank(data.getCategoryFirstName()) ? "" : data.getCategoryFirstName());
		builder.append(StringUtils.isBlank(data.getCategorySecondName()) ? "" : ">"+data.getCategorySecondName());
		holder.tv_category.setText(builder.toString());

		DisplayImageOptions options = new DisplayImageOptions.Builder()//
				.showImageForEmptyUri(R.drawable.ic_public_nophoto)
				.showImageOnFail(R.drawable.ic_public_nophoto)
				.imageScaleType(ImageScaleType.EXACTLY)
				.cacheInMemory(true)//
				.cacheOnDisk(true)//
				.bitmapConfig(android.graphics.Bitmap.Config.RGB_565)//
				.build();
		ImageLoader.getInstance().displayImage(data.getPicture(), holder.img, options);

		holder.tv_content.setText(data.getName());
		holder.tv_kc.setText("库存: "+data.getStockNumber());
		holder.tv_xl.setText("销量: "+data.getSaleNum());
		holder.tv_new_amt.setText("￥"+StringUtils.formatIntMoney(data.getPromotionPrice()));
		holder.tv_old_amt.setText("￥"+StringUtils.formatIntMoney(data.getNoVipPrice()));
		holder.tv_old_amt.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); // 中间横线
		holder.tv_btn_1.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (listener != null){
					listener.editStock(data.getId(), data.getIsLaunch(), data.getStockNumber());
				}
			}
		});

		holder.tv_btn_3.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (listener != null){
					listener.UpAndDown(data.getId(), data.getIsLaunch());
				}
			}
		});



		return convertView;
	}





	public final class ViewHolder {
		private TextView tv_time;
		private TextView tv_category;
		private ImageView img;
		private TextView tv_content;
		private TextView tv_kc;
		private TextView tv_xl;
		private TextView tv_new_amt;
		private TextView tv_old_amt;
//		private TextView tv_num;
		private LinearLayout ll_button;
		private TextView tv_btn_1;
		private TextView tv_btn_2;
		private TextView tv_btn_3;
	}

}
